package com.suray.basic.wcs.plc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.basic.wcs.plc.entity.LifterInfoDB;
import com.suray.basic.wcs.plc.mapper.LifterInfoDBMapper;
import com.suray.basic.wcs.plc.service.ILifterInfoDBService;
import com.suray.basic.wcs.plc.utils.LifterInfoType;
import com.suray.basic.wcs.plc.utils.LifterLocationType;
import com.suray.basic.wcs.plc.utils.PalletStatus;
import com.suray.basic.wcs.utils.Coord;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LifterInfoDBServiceImpl extends ServiceImpl<LifterInfoDBMapper, LifterInfoDB>
        implements ILifterInfoDBService {

    public List<LifterInfoDB> getAllLifterInfos(String series) {
        QueryWrapper<LifterInfoDB> eq = new QueryWrapper<>();
        eq.lambda().eq(LifterInfoDB::getSeries, series);
        return this.list(eq);
    }

    public List<LifterInfoDB> getLifterInfos(String series) {
        QueryWrapper<LifterInfoDB> eq = new QueryWrapper<>();
        eq.lambda().eq(LifterInfoDB::getSeries, series)
                .eq(LifterInfoDB::getType, LifterInfoType.LIFTER_LAYER.getValue());
        return this.list(eq);
    }

    public boolean updateLifterInfoByLayer(int layer, int dis, String series) {
        String location = String.valueOf(layer);

        UpdateWrapper<LifterInfoDB> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(LifterInfoDB::getSeries, series)
                .eq(LifterInfoDB::getLocation, location)
                .eq(LifterInfoDB::getType, LifterInfoType.LIFTER_LAYER.getValue())
                .set(LifterInfoDB::getDistance, dis);
        return this.update(updateWrapper);
    }

    public List<LifterInfoDB> getCarInfos(String series) {
        QueryWrapper<LifterInfoDB> eq = new QueryWrapper<>();
        eq.lambda().eq(LifterInfoDB::getSeries, series)
                .eq(LifterInfoDB::getType, LifterInfoType.TWO_DIR_CAR.getValue());
        return this.list(eq);
    }

    public boolean updateCarInfoByLayer(Coord node, LifterLocationType locationType, String groundCode, int dis,
                                        String series) {
        UpdateWrapper<LifterInfoDB> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(LifterInfoDB::getSeries, series)
                .eq(LifterInfoDB::getType, LifterInfoType.TWO_DIR_CAR.getValue())
                .eq(LifterInfoDB::getLocation, node.toString() + "_" + locationType.getDes())
                .set(LifterInfoDB::getDistance, dis)
                .set(LifterInfoDB::getGroundCode, groundCode);
        return this.update(updateWrapper);
    }

    public List<LifterInfoDB> getPalletInfos(String series) {
        QueryWrapper<LifterInfoDB> eq = new QueryWrapper<>();
        eq.lambda().eq(LifterInfoDB::getSeries, series)
                .eq(LifterInfoDB::getType, LifterInfoType.CAR_PALLET.getValue());
        return this.list(eq);
    }

    public boolean updatePalletInfoByLayer(PalletStatus palletStatus, int dis, String series) {
        UpdateWrapper<LifterInfoDB> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(LifterInfoDB::getSeries, series)
                .eq(LifterInfoDB::getLocation, String.valueOf(palletStatus.getStatusNum()))
                .eq(LifterInfoDB::getType, LifterInfoType.CAR_PALLET.getValue())
                .set(LifterInfoDB::getDistance, dis);
        return this.update(updateWrapper);
    }

    public void deleteLifterInfoBySeries(String series) {
        QueryWrapper<LifterInfoDB> eq = new QueryWrapper<>();
        eq.lambda().eq(LifterInfoDB::getSeries, series);
        List<LifterInfoDB> lifterInfoDBS = this.list(eq);
        List<Integer> ids = new ArrayList<>();
        for (LifterInfoDB lifterInfoDB : lifterInfoDBS) {
            ids.add(lifterInfoDB.getId());
        }
        this.removeByIds(ids);
    }

    public void deleteLifterDisInfoBySeries(String series, int type) {
        QueryWrapper<LifterInfoDB> eq = new QueryWrapper<>();
        eq.lambda().eq(LifterInfoDB::getSeries, series).eq(LifterInfoDB::getType, type);
        List<LifterInfoDB> lifterInfoDBS = this.list(eq);
        List<Integer> ids = new ArrayList<>();
        for (LifterInfoDB lifterInfoDB : lifterInfoDBS) {
            ids.add(lifterInfoDB.getId());
        }
        if(CollectionUtils.isNotEmpty(ids)){
            this.removeByIds(ids);
        }
    }
}
